package util;

import Controller.ControllerHandler;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class Scan {
    //Thread.currentThread().getConClassLoader().getResource("").getPath();
    private static final String CLASS_PATH= ControllerHandler.class.getClassLoader().getResource("").getPath();;
    private static List<String> list=new ArrayList<String>();
    public static List<String> getBeanName() throws UnsupportedEncodingException {

        //转码
        //String classChange = java.net.URLDecoder.decode(classPath, "utf-8");
        fun(CLASS_PATH);
        return list;
    }
    /**
     * 获取线程下的所有文件名
     */
    private static void fun(String classPath){
        File file=new File(classPath);
        File[] files = file.listFiles();
        if(files!=null){
            for(int i=0;i<files.length;i++){
                if(files[i].isFile()){
                    String s = subString(files[i].getAbsolutePath());
                    if(s!=null){
                        list.add(s);
                    }
                }else{
                    fun(files[i].getAbsolutePath());
                }
            }
        }
    }

    //带文件名的路径
    public static String subString(String str){
        String s=null;
        if(str.indexOf(".class")!=-1) {
            s = str.substring(CLASS_PATH.length() - 1, str.length()).replace(".class", "").replace(File.separator, ".");
        }
        return s;
    }
}
